package com.dmgis.qxfw.dataservice.businessservice.yydl.controller;

import com.dmgis.qxfw.dataservice.businessservice.common.entity.MsgMedium;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.WeatherMonitor;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.yydl.service.WeatherMonitorServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/xtdl")
public class WeatherMonitorController {

	@Autowired
	private WeatherMonitorServiceI weatherServiceImpl;

	// 天气监控预警初始化最新时间
	@RequestMapping("/initMonitorTime")
	@ResponseBody
	public String initMonitorTime() {
		Date d = weatherServiceImpl.initMonitorTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
		String strD = sdf.format(d);
		return strD+"|"+strD;
	}

	@RequestMapping("/weatherMonitor")
	@ResponseBody
	public List<WeatherMonitor> weatherMonitor(HttpServletRequest request) throws Exception {
		String strStartTime = request.getParameter("StartTime")+ ":00:00";
		String strEndTime = request.getParameter("EndTime") + ":00:00";
		String maxTemp = request.getParameter("MaxTemp");
		String minTemp = request.getParameter("MinTemp");
		String windVal = request.getParameter("WindVal");
		String rainVal = request.getParameter("RainVal");
		boolean bMainTable = false;
		if (DateTime.now().after(DateTime.parseDate(strStartTime).AddDays(30))
				|| DateTime.now().after(DateTime.parseDate(strEndTime).AddDays(30))) {
			bMainTable = true;
		}
		List<MsgMedium> list = new ArrayList<MsgMedium>();
		if (bMainTable) {
			list = weatherServiceImpl.getMsgMedium(strStartTime, strEndTime, maxTemp, minTemp, windVal, rainVal,bMainTable);
		}
		list = weatherServiceImpl.getMsgMedium(strStartTime, strEndTime, maxTemp, minTemp, windVal, rainVal, bMainTable);
		List<WeatherMonitor> listResult = new ArrayList<WeatherMonitor>();
		String temp0, maxtemp, mintemp, windv, rainval;
		float x, y;
		for (MsgMedium m : list) {
			DateTime dt = new DateTime(m.getObservTime());
			WeatherMonitor wm = new WeatherMonitor();
			wm.setStationId(m.getStationID());
			wm.setStationName(m.getStationName());
			wm.setObservTime(dt.toDateTimeString());
			maxtemp = String.valueOf(m.getMyMaxTemp()) == "-65535" ? "0" : String.valueOf(m.getMyMaxTemp());
			mintemp = String.valueOf(m.getMyMinTemp()) == "-65535" ? "0" : String.valueOf(m.getMyMinTemp());
			if (maxTemp.compareTo(maxtemp) <= 0) {
				temp0 = maxtemp;
			} else if (minTemp.compareTo(mintemp) >= 0) {
				temp0 = mintemp;
			} else {
				temp0 = "0";
			}
			temp0 = String.valueOf(Math.round(Double.valueOf(temp0) * 10.0) / 10.0);
			wm.setTemp(temp0);
			// 风速
			windv = String.valueOf(m.getWindVelocity()) == "-65535" ? "0" : String.valueOf(m.getWindVelocity());
			windv = String.valueOf(Math.round(Double.valueOf(windv) * 100.0) / 100.0);
			wm.setWindV(windv);
			// 雨量
			NumberFormat nf=new DecimalFormat( "0.0 ");
			rainval = String.valueOf(Double.parseDouble(nf.format(m.getRain()))) == "-65535" ? "0" : String.valueOf(Double.parseDouble(nf.format(m.getRain())));
			wm.setRain(rainval);
			x = m.getLongitude();
			y = m.getLatitude();
			wm.setLon(x);
			wm.setLat(y);
			wm.setY(m.getX());
			wm.setX(m.getY());
			listResult.add(wm);
		}
		return listResult;
	}
}
